package com.orangelife.mobile.common.view;

/**
 * 只有一个输入框的Dialog
 */
import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;

public class EdittextDialog extends Dialog {
	
	private Activity activity;

	public EdittextDialog(Activity activity) {
		super(activity,R.style.UIAlertViewStyle);
		
		this.activity = activity;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		inite();
	}

	/**
	 * 做初始化数据
	 */
	private void inite() {
		LayoutInflater inflater = LayoutInflater.from(activity);
		View view = inflater.inflate(R.layout.dialog_add_building, null);
		setContentView(view);
		
		EditText etCommunity = (EditText) findViewById(R.id.etBuilding);
		
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = activity.getResources().getDisplayMetrics();
		
		lp.width = (int) (d.widthPixels * 0.8);
		dialogWindow.setAttributes(lp);
		
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		
		if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){  
            /*隐藏软键盘*/  
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);  
            if(inputMethodManager.isActive()){  
                inputMethodManager.hideSoftInputFromWindow( activity.getCurrentFocus().getWindowToken(), 0);  
            }  
            
            setCommunity();
            return true;  
        }  
        return super.dispatchKeyEvent(event);  
	}

	@SuppressWarnings("deprecation")
	private void setCommunity() {
		Map<String,Object> map = new HashMap<String, Object>();
		
		
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url", Constant.URL_COMPLAINT_DECLARATION);
		rootMap.put("wat", 2);
		new JSONRequest(JSONRequest.POST, rootMap, handler,
				HttpUtil.ACTION_JSON);
	}
	
	/**
	 * 接收处理线程返回的消息
	 */
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.WHAT_ERROR_HTTP:
				ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
				break;
			
			case 2:
			 String	errInfo = JSONHelper.jsonToMap(msg.obj.toString())
						.get("errInfo").toString();
				if (errInfo.equals("OK")) {
					ToastHelper.getInstance()._toast(activity.getResources().getString(R.string.add_community_success));
					dismiss();
					
				}
				break;
			}
		};
	};

}
